package hello.word.m2.maven.domain;

import hello.word.m2.maven.support.AbstractEntity;
/*import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;
import lombok.ToString;*/

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Table;
import java.beans.ConstructorProperties;
import java.io.UnsupportedEncodingException;
import java.time.LocalDateTime;

//@Data
//@EqualsAndHashCode(callSuper = false)
//@ToString
//@NoArgsConstructor
@Entity
@Table(name = "t_book")
public class Book extends AbstractEntity<Long> {

	@Column(name = "id")
	private Long id;
	@Column(name = "name")
	private String firstName;
	@Column(name = "publish_datetime")
	private LocalDateTime publishDateTime;

	//also worked as below
	/*@JdbiConstructor
	public Book(@ColumnName("id")Long id, @ColumnName("name")String firstName,@ColumnName("publish_datetime") LocalDateTime publishDateTime) {
		this.id = id;
		this.firstName = firstName;
		this.publishDateTime = publishDateTime;
	}*/

    public Book() {
    }

    @ConstructorProperties({"id", "name","publish_datetime"})
	public Book(Long id, String firstName, LocalDateTime publishDateTime) {
		this.id = id;
		this.firstName = firstName;
		this.publishDateTime = publishDateTime;
	}

	@Override
	public Long id() {
		return id;
	}

    public static void main(String[] args) throws UnsupportedEncodingException {
//        Arrays.toString()
        String hex="FFFFFFFF02000100FFFFFFFF13000000FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF110053302D453133332D53312D56417C7C307C";
        byte[] bytes=hexToByteArray(hex);
        String de = new String(bytes);
        String utf8 = new String(bytes,"UTF-8");
        String gbk = new String(bytes,"GBK");
        String iso = new String(bytes,"ISO-8859-1");
        String GB18030 = new String(bytes,"GB18030");
        System.out.println("de: "+de);
        System.out.println("utf8: "+utf8);
        System.out.println("gbk: "+gbk);
        System.out.println("iso: "+iso);
        System.out.println("GB18030: "+GB18030);
//        System.out.println(Arrays.toString(hexToByteArray("FFAA")));
    }

    public static byte[] hexToByteArray(String inHex){
        int hexlen = inHex.length();
        byte[] result;
        if (hexlen % 2 == 1){
            //奇数
            hexlen++;
            result = new byte[(hexlen/2)];
            inHex="0"+inHex;
        }else {
            //偶数
            result = new byte[(hexlen/2)];
        }
        int j=0;
        for (int i = 0; i < hexlen; i+=2){
            result[j]=hexToByte(inHex.substring(i,i+2));
            j++;
        }
        return result;
    }

    public static byte hexToByte(String inHex){
        Integer num=Integer.parseInt(inHex,16);
//        System.out.print(Integer.toBinaryString(num));
        return (byte)Integer.parseInt(inHex,16);
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public LocalDateTime getPublishDateTime() {
        return publishDateTime;
    }

    public void setPublishDateTime(LocalDateTime publishDateTime) {
        this.publishDateTime = publishDateTime;
    }
}
